package com.bana.fvs.app.module.info.page;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.enter.NewVersionBean;
import com.bana.fvs.app.mvp.contract.NewVersionContract;
import com.bana.fvs.app.mvp.presenter.NewVersionPresenter;
import com.bana.fvs.app.util.DialogManager;
import com.wei.commonlibrary.dialog.BaseDialog;
import com.wei.commonlibrary.dialog.DownLoadManager;
import com.wei.commonlibrary.dialog.VersionUpdateDialog;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.AppUtils;
import com.wei.commonlibrary.utils.ToastUtil;

import java.io.File;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/2/8
 * Time   : 11:39
 * Desc   : 关于
 */
// TODO: 2018/2/11 版本更新的弹框，样式待定  没改呢
public class AboutActivity extends BaseActivity<NewVersionPresenter> implements NewVersionContract.INewVersionView {
    private static final String TAG = "dah_AboutActivity";
    @BindView(R.id.tvCurrentVersion)
    TextView mTvCurrentVersion;
    @BindView(R.id.tvUpdateVersion)
    TextView mTvUpdateVersion;
    private boolean hasNewVersion = false;
    private NewVersionBean mNewVersionBean;


    @Override
    public int setLayout() {
        return R.layout.activity_about;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setTitleText(getString(R.string.about));
        mBaseTitleBarView.setLeftDrawable(-1);
        mBaseTitleBarView.setLeftTitleGone();
    }

    @Override
    public void initWidget() {
        String versionIfo = getVersionIfo();
        if (!TextUtils.isEmpty(versionIfo)) {
            mTvCurrentVersion.setText(versionIfo);
        }
        mTvUpdateVersion.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tvUpdateVersion:
                if (hasNewVersion) {
                    if (null != mNewVersionBean) {
                        showDescDialog(mNewVersionBean);
                    }
                } else {
                    ToastUtil.showLong(mContext, getString(R.string.tip_latest_version));
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
    }

    @Override
    public void onResponse(NewVersionBean versionBean) {
        //赋值是否有新版本
        this.hasNewVersion = versionBean.isHasNewVersion();
        this.mNewVersionBean = versionBean;
    }

    @Override
    public void onStop() {
        super.onStop();
        if (updateDialog != null) {
            updateDialog.dismiss();
        }
    }

    private String getVersionIfo() {
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
            String versionName = packageInfo.versionName;
            return String.format("%s%s", "V", versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    VersionUpdateDialog updateDialog = null;

    /**
     * 更新弹框
     */
    private void showDescDialog(final NewVersionBean newVersionBean) {
        BaseDialog.OnDialogListener dialogListener = new BaseDialog.OnDialogListener() {
            @Override
            public void rightClick(String text) {
                if (updateDialog != null) {
                    updateDialog.dismiss();
                }
            }

            @Override
            public void leftClick() {
                //取消-->跳转相应的页面
                if (updateDialog != null) {
                    updateDialog.dismiss();
                }
                //更新
                DownLoadManager.startDownload((AboutActivity) mContext, newVersionBean.getUrl(), new DownLoadManager.OnDownloadListener() {
                    @Override
                    public void onDownSuccess(File apkFile) {
                        //安装
                        AppUtils.installApk((AboutActivity) mContext, apkFile);
                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(CommException exception) {
                        ToastUtil.showShort(mContext, exception.getMessage());
                    }
                });
            }
        };
        updateDialog = DialogManager
                .createUpdateDialog(this, newVersionBean, dialogListener);
        updateDialog.show();
    }
}
